<?php

class BankHelper extends Helper {

    private $check_rule;
    private $_bkfield;
    private $rate_rule;
    private $_rtfield;
    private $rc_rule;
    protected $loan_mode;

    public function __construct() {
        $this->check_rule = array(
            'bk_code' => array('require', '银行编号不能为空'),
            'bk_name' => array('require', '银行名称不能为空'),
            'bk_newloan_in' => array('number', '省内新车实际贷款比例上限值错误'),
            'bk_newloan_out' => array('number', '省外新车实际贷款比例上限值错误'),
            'bk_secloan_in' => array('number', '省内二手车实际贷款比例上限值错误'),
            'bk_secloan_out' => array('number', '省外二手车实际贷款比例上限值错误'),
            'bk_newstage_in' => array('number', '省外新车申请分期付款总额比例值错误'),
            'bk_newstage_out' => array('number', '省外新车申请分期付款总额比例值错误'),
            'bk_secstage_in' => array('number', '省内二手车申请分期付款总额比例值错误'),
            'bk_secstage_out' => array('number', '省外二手车申请分期付款总额比例值错误'),
            'bk_newratio_in' => array('number', '省外新车分期付款总额比例上限值错误'),
            'bk_newratio_out' => array('number', '省内新车分期付款总额比例上限值错误'),
            'bk_secratio_in' => array('number', '省外二手车分期付款总额比例上限值错误'),
            'bk_secratio_out' => array('number', '省内二手车分期付款总额比例上限值错误'),
        );
        $this->_bkfield = array('bk_id', 'bk_code', 'bk_name', 'bk_abbreviation', 'bk_newloan_in', 'bk_newloan_out', 'bk_secloan_in', 'bk_secloan_out', 'bk_newstage_in', 'bk_newstage_out', 'bk_secstage_in', 'bk_secstage_out', 'bk_newratio_in', 'bk_newratio_out', 'bk_secratio_in', 'bk_secratio_out', 'bk_inpro', 'bk_online_verify_code', 'bk_can_online_verify');
        $this->rate_rule = array(
            'rt_bank_id' => array('number', '银行不能为空'),
            'rt_type' => array('require', '产品类型不能为空'),
            'rt_ylimit' => array('number', '年限不能为空'),
            'rt_newcode_in' => array('require', '省内新车代码不能为空'),
            'rt_newrate_in' => array('number', '省内新车利率错误'),
            'rt_seccode_in' => array('require', '省内二手车代码不能为空'),
            'rt_secrate_in' => array('number', '省内二手车利率错误'),
            'rt_newcode_out' => array('require', '省外新车代码不能为空'),
            'rt_newrate_out' => array('number', '省外新车利率错误'),
            'rt_seccode_out' => array('require', '省外二手车代码不能为空'),
            'rt_secrate_out' => array('number', '省外二手车利率错误'),
            //'rt_newloan_limit' => array('number', '新车贷款上限错误'),
            //'rt_secloan_limit' => array('number', '二手车贷款上限错误'),
            'rt_up_time' => array('require', '启用时间不能为空')
        );
        $this->_rtfield = array('rt_id', 'rt_bank_id', 'rt_bank_code', 'rt_type', 'rt_ylimit', 'rt_newcode_in', 'rt_newrate_in', 'rt_seccode_in', 'rt_secrate_in', 'rt_newcode_out', 'rt_newrate_out', 'rt_seccode_out', 'rt_secrate_out', 'rt_newloan_limit', 'rt_secloan_limit', 'rt_up_time');
        $this->rc_rule = array(
            'o_bank_id' => array('number', '银行值错误'),
            'ot_car_pose' => array('require', '车型构成不能为空'),
            'ot_loan_type' => array('require', '产品类型不能为空'),
            'ot_loan_year' => array('number', '贷款年限不能为空'),
            'ot_car_price' => array('number', '车价不能为空'),
            'ot_loan_limit' => array('number', '贷款额度不能为空'),
            'ot_loan_rate' => array('number', '公司费率不能为空'),
            'ot_reg_province' => array('require', '上牌地省份不能为空')
        ); //银行贷款计算输入值校验

        $mode = Config::GetCommon();
        $this->loan_mode = $mode['mode'];
    }

    /**
     * 银行保存
     * @param type $data
     * @param type $where
     * @return type
     */
    public function bankSave($data, $where = '') {
        if (is_numeric($where)) {
            $where = array('`bk_id` = ?' => $where);
        }
        $validator = Validator::checkRule($data, $this->check_rule);
        if (!$validator['pass']) {
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $bankModel = $this->LoadModel('Public');
        if (empty($where)) {
            $id = $bankModel->save('bank', $data);
            if ($id) {
                return array('success' => true, 'msg' => $id);
            } else {
                return array('success' => false, 'msg' => '保存失败');
            }
        } else {
            $bankModel->update('bank', $data, $where);
            return array('success' => true);
        }
    }

    /**
     * 分页
     * @param array $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function bankList($where, $page = 1, $limit = PAGE_SIZE, $param = array()) {
        $bankModel = $this->LoadModel('Public');
        $data['count'] = $bankModel->getRowCount('bank', $where);
        if (!empty($data['count'])) {
            $data['all'] = $bankModel->getList('bank', $this->_bkfield, $where, array($page, $limit), NULL, array('bk_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }

    /**
     * 单条数据
     * @param type $where
     * @return type
     */
    public function bankRow($where) {
        if (is_numeric($where)) {
            $where = array('`bk_id` = ?' => $where);
        }
        $bankModel = $this->LoadModel('Public');
        $data = $bankModel->getOneRow('bank', $this->_bkfield, $where);
        return $data;
    }

    /**
     * 
     * @param type $where
     * @return type
     */
    public function bankAll($where) {
        if (is_numeric($where)) {
            $where = array('`bk_id` = ?' => $where);
        }
        $bankModel = $this->LoadModel('Public');
        $data = $bankModel->getList('bank', $this->_bkfield, $where);
        return $data;
    }

    /**
     * 删除
     * @param type $where
     * @return type
     */
    public function bankRemove($where) {
        if (is_numeric($where)) {
            $where = array('`bk_id` = ?' => $where);
        }
        $bankModel = $this->LoadModel('Public');
        $do = $bankModel->update('bank', array('bk_status' => 'del'), $where);
        return $do;
    }

    /**
     * 银行利率保存
     * @param type $data
     * @param type $where
     * @return type
     */
    public function rateSave($data, $where = '') {
        if (is_numeric($where)) {
            $where = array('`rt_id` = ?' => $where);
        }
        $validator = Validator::checkRule($data, $this->rate_rule);
        if (!$validator['pass']) {
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $rateModel = $this->LoadModel('Public');
        if (empty($where)) {
            $id = $rateModel->save('bank_rate', $data);
            if ($id) {
                return array('success' => true, 'msg' => $id);
            } else {
                return array('success' => false, 'msg' => '保存失败');
            }
        } else {
            $rateModel->update('bank_rate', $data, $where);
            return array('success' => true);
        }
    }

    public function rateList($where, $page = 1, $limit = PAGE_SIZE, $param = array()) {
        $rateModel = $this->LoadModel('Public');
        $data['count'] = $rateModel->getRowCount('bank_rate', $where);
        if (!empty($data['count'])) {
            $data['all'] = $rateModel->getList('bank_rate', $this->_rtfield, $where, array($page, $limit), NULL, array('rt_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }
    
    public function rateAll($where, $group = null, $order = array('rt_id ASC')){
        if (is_numeric($where)) {
            $where = array('`rt_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $rate_all = $model->getList('bank_rate', $this->_rtfield, $where, NULL, $group, $order);
        return $rate_all;
    }

    public function rateRow($where) {
        if (is_numeric($where)) {
            $where = array('`rt_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $data = $model->getOneRow('bank_rate', $this->_rtfield, $where);
        return $data;
    }

    public function rateRemove($where) {
        if (is_numeric($where)) {
            $where = array('`rt_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $do = $model->update('bank_rate', array('rt_status' => 'del'), $where);
        return $do;
    }

    /**
     * @param $bankId 银行ID
     * @param $bankName 银行名称
     * @param $procName 银行产品名称*
     * @param $loanAge 贷款期数
     * @param $province 上牌地省
     * @param $carType 新车or二手车
     * @param $carPrice 车价
     * @param $oldPrice 评估价
     * @param $firstRegDate 初始登记日时间戳
     * @param $otherPrice 其他费用
     * @param $loanMoney 贷款金额
     * @param $companyRate 公司费率%
     * @param $serviceRate 服务费率%
     * @return mixed
     */
    public function rateNewCompute($bankId, $bankName,
                                   $procName='信用卡分期(利息分期)',
                                   $loanAge='36', $province,
                                   $carType='新车', $carPrice,
                                   $oldPrice=0,
                                   $firstRegDate=0, $otherPrice=0, $loanMoney, $companyRate, $serviceRate){
        $result = array();
        $bankNames = array(
            //'中国农业银行股份有限公司萧山分行',
            //'中国工商银行股份有限公司杭州延中支行',
            //'（2017）中国工商银行股份有限公司杭州延中支行',
            //'中国工商银行股份有限公司南京城南支行',
            //'（长马）中国工商银行股份有限公司南京城南支行'
            '中国工商银行股份有限公司哈尔滨东直支行'
        );
        $loanAges = array('12', '24', '36');
        $carTypes = array('新车','二手车');
        if(!in_array($bankName, $bankNames)) return array('success' => false, 'msg' => '请选择正确的银行');
        if(!in_array($loanAge, $loanAges)) return array('success' => false, 'msg' => '请选择正确的贷款期数');
        if(!in_array($carType, $carTypes)) return array('success' => false, 'msg' => '请选择正确的车辆构成');
        if(fmod(bcmul($carPrice,1000), 10)>0) return array('success' => false, 'msg' => '车价只可填写到分');
        if(fmod(bcmul($oldPrice,1000), 10)>0) return array('success' => false, 'msg' => '评估价只可填写到分');
        if($carType == '新车' && $oldPrice>0) return array('success' => false, 'msg' => '新车无需填写评估价');
        if(fmod(bcmul($otherPrice,1000), 10)>0) return array('success' => false, 'msg' => '其他费用只可填写到分');
        if(fmod($loanMoney,1000)>0) return array('success' => false, 'msg' => '贷款额必须为1000的整数倍');
        if(fmod(bcmul($companyRate,10000), 10)>0) return array('success' => false, 'msg' => '公司费率只可填写到小数点后3位');
        if(fmod(bcmul($serviceRate,10000), 10)>0) return array('success' => false, 'msg' => '服务费率只可填写到小数点后3位');

        $rateinfoWhere['o_bank_id'] = $bankId;
        $rateinfoWhere['ot_loan_type'] = $procName;
        $rateinfoWhere['ot_loan_year'] = $loanAge;
        $rateinfoWhere['ot_reg_province'] = $province;
        $rateinfoWhere['ot_car_pose'] = $carType;
        $rateinfo = $this->rateBack($rateinfoWhere);//获取执行的银行产品、利率、贷款比例上限、分期付款总额比例上限、申请分期付款总额比例上限

        if($rateinfo['data']['bank'] != $bankName) return array('success' => false, 'msg' => '银行参数错误');

        //特别限制，以及特别比例的调整
        if($bankName != '中国农业银行股份有限公司萧山分行' && $otherPrice > 0) return array('success' => false, 'msg' => $bankName.'，其他费用只能为0');
        if(bcmul($rateinfo['data']['rate'],100,3) != bcsub($companyRate,$serviceRate,3)) return array('success' => false, 'msg' => '公司费率、服务费率、银行利率不匹配');
        if($serviceRate<0) return array('success' => false, 'msg' => '公司费率不能低于银行利率' . $rateinfo['data']['rate'] * 100);

        if($bankName == "中国工商银行股份有限公司哈尔滨东直支行"){
            if($province == '江苏省') return array('success' => false, 'msg' => '不支持上牌地为江苏省');

            $result['d_payment'] = bcsub($carPrice, $loanMoney);//实际首付款
            $result['d_service_money'] = bcmul(ceil(bcdiv(bcmul($serviceRate , $loanMoney), 10000, 4)),100);//金融服务费
            $result['d_signag_money'] = bcadd($result['d_service_money'], $loanMoney);//申请分期付款总额
            $result['d_aging_money'] = bcmul($result['d_signag_money'], bcadd(1, $rateinfo['data']['rate'], 5), 2);//分期付款总额
            $result['d_transaction_amount'] = $carPrice;//出报告金额
            $result['d_aging_poundage'] = bcsub($result['d_aging_money'], $result['d_signag_money'], 2); //分期手续费

            $result['d_refund_month'] = floor(bcdiv($result['d_signag_money'], $loanAge, 5)) + floor(bcdiv($result['d_aging_poundage'], $loanAge, 5)); //月还款
            $result['d_refund_1month'] = bcsub($result['d_aging_money'], bcmul($result['d_refund_month'],bcsub($loanAge, 1)), 2);//首期还款

            $result['d_payment_per'] = bcmul(bcdiv($result['d_payment'], $carPrice, 4), 100, 2);//实际首付比例
            $result['d_loan_per'] = bcsub(100, $result['d_payment_per'], 2);//实际贷款比例
            $result['d_aging_moneyper'] =  bcmul(bcdiv($result['d_signag_money'], $carPrice, 4), 100, 2);//申请分期付款总额比例
            $result['d_aging_per'] = bcmul(bcdiv($result['d_aging_money'], $carPrice, 4), 100, 2); //分期付款比例
            $result['d_service_per'] = bcmul(bcdiv($result['d_service_money'], $carPrice, 4), 100, 2); //金融服务费比例

            $result['d_type'] = '预审';//审核模式，预审为：需合同原件审核通过后垫款
            if(bccomp($result['d_aging_money'], 500000, 0)==-1) $result['d_type'] = '超级预审';//审核模式，超级预审为：凭提交的电子件审核通过后即可垫款

            if(bccomp($loanMoney, 20000, 0)==-1) return array('success' => false, 'msg' => "贷款额不能低于银行限定的2万元");
            if(bccomp($result['d_aging_money'], 1000000, 0)==1) return array('success' => false, 'msg' => "分期付款总额不能高于银行限定的100万元");
            if(bccomp($result['d_service_per'], 15, 2)==1) return array('success' => false, 'msg' => "金融服务费不能超过银行限定的15%");

            //return array('success' => false, 'msg' => "实际首付款".$result['d_payment']."<br />金融服务费".$result['d_service_money']."<br />申请分期付款总额".$result['d_signag_money'].
            //"<br />分期付款总额".$result['d_aging_money']."<br />出报告金额".$result['d_transaction_amount']."<br />分期手续费".$result['d_aging_poundage']
            //."<br />月还款".$result['d_refund_month']."<br />首期还款".$result['d_refund_1month']);
            if($carType == "二手车"){
                if(bccomp(bcdiv($loanMoney,$oldPrice,9), 0.7, 9)==1) return array('success' => false, 'msg' => "实际贷款比例（评估价）超银行限定的70%");
                $result['d_transaction_amount'] = ceil(bcdiv(bcdiv($result['d_signag_money'],0.7,4),1000,7))*1000;//出报告金额
                if(bccomp(bcdiv($result['d_signag_money'], min($result['d_transaction_amount'], $carPrice), 9), 0.7, 9)==1) return array('success' => false, 'msg' => "申请分期比例超银行限定的70%");
                if(bccomp(bcdiv($result['d_service_money'], min($result['d_transaction_amount'], $carPrice), 9), 0.15, 9)==1) return array('success' => false, 'msg' => "金融服务费不能超过银行限定的15%");
                $result['d_car_loanold'] = bcadd(bcsub(time(), strtotime($firstRegDate), 0), bcmul(bcdiv($loanAge,12,0),31536000,0), 0);//年限
                if(bccomp($result['d_signag_money'], 100000, 2)==-1){
                    if(bccomp($result['d_car_loanold'],bcmul(13,31536000,0), 0)==1) return array('success' => false, 'msg' => "申请分期付款金额10万以下年限为初始上牌起加贷款年限不能超过13年");
                }else{
                    if(bccomp($result['d_car_loanold'],bcmul(11,31536000,0), 0)==1) return array('success' => false, 'msg' => "申请分期付款金额10万（含）以上年限为初始上牌起加贷款年限不能超过11年");
                }
            }else{

            }
        }
        return array('success' => true, 'msg' => '请求成功', 'data' => $result);
    }

    /*if($bankName == '中国农业银行股份有限公司萧山分行'){
            if($carPrice*0.1<$otherPrice) return array('success' => false, 'msg' => '中国农业银行股份有限公司萧山分行，其他费用不能超出车价的10%');
            if($carType == '新车'){
                if($carPrice>150000) {
                    if ($otherPrice > 0) return array('success' => false, 'msg' => '中国农业银行股份有限公司萧山分行，新车，车价为15万以上，其他费用只能为0');
                    $rateinfo['data']['loan_limit'] = 70;
                }else{
                }
            }else{
                if($oldPrice!=$carPrice) return array('success' => false, 'msg' => '中国农业银行股份有限公司萧山分行，二手车，车价必须等于评估价');
            }
        }

        //限制车价
        $limitCarPrice = $carPrice;
        if($bankName == '中国农业银行股份有限公司萧山分行') $limitCarPrice=$carPrice+$otherPrice;
        else{
            //if($carType == '二手车')
        }


        echo  var_dump($rateinfo);
        //return $rateinfo;*/

    /**
     * 根据参数进行利率计算
     * @param type $data
     * @return type
     * decimal 银行费率 = rate.FAmt.Value / 100;签约银行配置(费率)
      decimal 实际首付款 = 车价 - 实际贷款额;
      decimal 实际首付比例 = 实际首付款 / 车价;
      decimal 实际贷款比例 = 实际贷款额 / 车价;
      decimal 申请分期付款总额 = Math.Ceiling(实际贷款额 * (1 + 公司费率) / (1 + 银行费率) / 100) * 100;
      decimal 申请分期付款总额比例 = 申请分期付款总额 / 车价;
      decimal 分期付款总额 = Math.Ceiling(申请分期付款总额 * (1 + 银行费率));
      decimal 分期付款总额比例 = 分期付款总额 / 车价;
      decimal 每月还款额 = Math.Ceiling(分期付款总额 / year);
      decimal 分期手续费 = 分期付款总额 - 申请分期付款总额;
      if (itemType == "信用卡分期(利息一次性)")
      {
      分期付款总额 = Math.Ceiling(实际贷款额 * (1 + 公司费率));
      分期付款总额比例 = 分期付款总额 / 车价;
      每月还款额 = Math.Ceiling(分期付款总额 / year);
      分期手续费 = Math.Ceiling(分期付款总额 * 银行费率);
      申请分期付款总额 = 分期付款总额;
      申请分期付款总额比例 = 分期付款总额比例;
      }
     */
    public function rateCompute($data, $car_price = 0) {
		$validator = Validator::checkRule($data, $this->rc_rule);
        if (!$validator['pass']) return array('success' => false, 'msg' => $validator['msg']);
		
		if(fmod($data['ot_loan_limit'],1000)>0) return array('success' => false, 'msg' => '贷款额必须为1000的整数倍');
		if(fmod(bcmul($data['ot_car_price'],1000), 10)>0) return array('success' => false, 'msg' => '车价只可填写到分');
		if(fmod(bcmul($data['ot_car_oprice'],1000), 10)>0) return array('success' => false, 'msg' => '其他费用只可填写到分');
		if(fmod(bcmul($data['ot_loan_rate'],10000), 10)>0) return array('success' => false, 'msg' => '公司费率只可填写到小数点后3位');
		
		
		$model = $this->LoadModel('Public');
        $car_row = $model->getOneRow('order_car', '*', array('`ot_oid` = ?' => $data['oid']));
        $data['ot_assessment'] = isset($data['ot_assessment']) ? $data['ot_assessment'] : $car_row['ot_assessment'];
        //if($data['o_bank_id']=='8') $data['ot_assessment']=$data['ot_car_price'];//萧山农行评估价=车价
		if($data['ot_car_pose'] == '二手车' && empty($data['ot_assessment'])) return array('success' => false, 'msg' => '请联系公司评估员录入评估价');
		//$data['ot_2car_price'] = min($data['ot_car_price'], $data['ot_assessment']);//2手车进行判断的车价
		
		
		$rateinfo = $this->rateBack($data);
		//（2017）中国工商银行股份有限公司杭州延中支行 //sunning改2017 96800为金望洲购车订单号
		if($data['o_bank_id']=='13' && $data['oid'] != 96800){
			if($rateinfo['data']['bank'] != '（2017）中国工商银行股份有限公司杭州延中支行') return array('success' => false, 'msg' => '银行配置错误');
			if($data['ot_car_pose'] == '新车' && $data['ot_loan_limit']>120000 && bcdiv($data['ot_loan_limit'], $data['ot_car_price'], 10)>0.70) return array('success' => false, 'msg' => '新车贷款额为12万以上，实际贷款比例不能超出70%');
            //if($data['ot_loan_limit']>500000 && $d_service_per>10) return array('success'=>false,'msg'=>'实际贷款额大于50W，金融服务费比例不能大于10%');
            //if($data['ot_reg_province'] == '江苏省') return array('success' => false, 'msg' => '不支持上牌地为江苏省');
		}

        if($data['o_bank_id']=='15'){
            if($rateinfo['data']['bank'] != '中国工商银行股份有限公司哈尔滨东直支行') return array('success' => false, 'msg' => '银行配置错误');
            //if($data['ot_car_pose'] == '新车' && $data['ot_loan_limit']>120000 && bcdiv($data['ot_loan_limit'], $data['ot_car_price'], 10)>0.70) return array('success' => false, 'msg' => '新车贷款额为12万以上，实际贷款比例不能超出70%');
            //if($data['ot_reg_province'] == '江苏省') return array('success' => false, 'msg' => '不支持上牌地为江苏省');
        }

        //中国农业银行股份有限公司萧山分行
        if($data['o_bank_id']=='8'){
            if($rateinfo['data']['bank'] != '中国农业银行股份有限公司萧山分行') return array('success' => false, 'msg' => '银行配置错误');
            if($data['ot_car_price']*0.1<$data['ot_car_oprice']) return array('success' => false, 'msg' => '其他费用不能超出车价的10%');
            if($data['ot_car_pose'] == '新车'){
                if($data['ot_car_price']>150000) {
                    if ($data['ot_car_oprice'] > 0) return array('success' => false, 'msg' => '新车车价为15万以上，其他费用只能为0');
                    if(bcdiv($data['ot_loan_limit'], $data['ot_car_price'], 10)>0.70) return array('success' => false, 'msg' => '新车车价为15万以上，贷款额不能超出车价的70%');
                }else{
                    if(bcdiv($data['ot_loan_limit'], $data['ot_car_price'], 10)>0.80) return array('success' => false, 'msg' => '新车车价为15万以下，贷款额不能超出车价的80%');
                }
            }else{
                if(bcdiv($data['ot_loan_limit'], $data['ot_car_price'], 10)>0.70) return array('success' => false, 'msg' => '二手车，贷款额不能超出车价的70%');
            }
        }

        //中国工商银行股份有限公司杭州延中支行
        if($data['o_bank_id']=='7'){
            if($rateinfo['data']['bank'] != '中国工商银行股份有限公司杭州延中支行'){
                return array('success' => false, 'msg' => '银行配置错误');
            }
        }
		
		//中国工商银行股份有限公司南京城南支行
		if($data['o_bank_id']=='11'){
			if($rateinfo['data']['bank'] != '中国工商银行股份有限公司南京城南支行'){
				return array('success' => false, 'msg' => '银行配置错误');
			}
		}
		
		//（长马）中国工商银行股份有限公司南京城南支行
		if($data['o_bank_id']=='12'){
			if($rateinfo['data']['bank'] != '（长马）中国工商银行股份有限公司南京城南支行'){
				return array('success' => false, 'msg' => '银行配置错误');
			}
		}

        //其他费用的处理
		if($data['ot_car_price']*0.1<$data['ot_car_oprice']) return array('success' => false, 'msg' => '其他费用不能超出车价的10%');
        $CARPRICE = $data['ot_car_price'];//真实车价



        $model = $this->LoadModel('Public');
        $car_row = $model->getOneRow('order_car', '*', array('`ot_oid` = ?' => $data['oid']));
        $data['ot_assessment'] = $car_row['ot_assessment'];
        //if($data['o_bank_id']=='8') $data['ot_assessment']=$data['ot_car_price'];//萧山农行评估价=车价
        if ($data['ot_car_pose'] == '二手车') {
            if (empty($data['ot_assessment']))
                return array('success' => false, 'msg' => '请联系公司评估员录入评估价');
                if($car_price > 0){
                    $data['ot_car_price'] = $car_price;
                }else{
                    $ot_car_price = $data['ot_car_price'];
                    if($rateinfo['data']['bank'] != '中国工商银行股份有限公司哈尔滨东直支行')//2018-6-12 调整哈工车价不取底
                        $data['ot_car_price'] = min($data['ot_car_price'], $data['ot_assessment']);
                }
        }
        $data['ot_car_price'] = $data['ot_car_price']+$data['ot_car_oprice']; //概念车价

        $rateinfo = $this->rateBack($data);
        if ($rateinfo['success']) {
            
            if(isset($rateinfo['data']['company_rate']) && $rateinfo['data']['company_rate'] != $data['ot_loan_rate']){
                return array('success' => false, 'msg' => '当前银行产品公司利率只能为'.$rateinfo['data']['company_rate'].'%');
            }
            $loan_money_limit = ($rateinfo['data']['loan_limit'] / 100) * $data['ot_car_price'];  //可贷款金额
            $loan_money_limit = sprintf("%.2f", $loan_money_limit);
            if ($data['ot_loan_limit'] > $loan_money_limit) {
                return array('success' => false, 'msg' => '贷款金额超出贷款比例' . $rateinfo['data']['loan_limit'] . '%');
            }
            if ($data['ot_loan_rate'] < $rateinfo['data']['rate'] * 100) {
                return array('success' => false, 'msg' => '公司费率不能低于银行利率' . $rateinfo['data']['rate'] * 100);
            }

            $post = array(
                'cr_bank_money' => $rateinfo['data']['bank'],
                'cr_genre_num' => $rateinfo['data']['code'],
                'back_rate' => $rateinfo['data']['rate'] * 100,
                'cr_company_rate' => $data['ot_loan_rate'],
                'cr_money' => $data['ot_car_price'],
                'cr_loan' => $data['ot_loan_limit'],
                'cr_ageid' => $rateinfo['data']['ylimit'],
                'cr_genreid' => $data['ot_loan_type']
            );

            /*if ($post['cr_bank_money'] == '中国农业银行股份有限公司萧山分行' && $post['cr_genre_num'] == '30002074') {
                $post['back_rate'] = 8.5;
            }*/
            //todo 此处需要用银行ID查出银行所给你费率
            $back_rate_b = floor($post['back_rate'] * 10000);
            $back_rate = ($back_rate_b) / 1000000;
            $company_rate = bcdiv($post['cr_company_rate'],100,5);
            $post['cr_down'] = $post['cr_money'] - $post['cr_loan']; //首付款
            $rate_data = array();
            $rate_data['company_rate'] = $company_rate;
            $rate_data['cr_bank_rate'] = $back_rate * 100;
            $rate_data['cr_company_rate'] = $post['cr_company_rate'];
            $rate_data['cr_down'] = $post['cr_down'];
            $rate_data['cr_down_ratio'] = number_format(StringCode::ceil_format(($post['cr_down'] / $post['cr_money']) * 100), 2, '.', ''); //首付比例
            $rate_data['cr_loan_ratio'] = number_format(StringCode::ceil_format(($post['cr_loan'] / $post['cr_money']) * 100), 2, '.', ''); //实际贷款比例
            //$data['cr_age_total']=ceil(($post['cr_loan']*$company_rate+$post['cr_loan'])/(1+$back_rate)/100)*100;//申请分期付款总额
            $crAgeTotal = floor(($post['cr_loan'] * $company_rate + $post['cr_loan']) / (1 + $back_rate) * 10000) / 1000000;
            $rate_data['cr_age_total'] = ceil($crAgeTotal) * 100; //申请分期付款总额
            if ($post['cr_bank_money'] == '中国农业银行股份有限公司萧山分行') {//sunning 2016-01-10
                $rate_data['cr_refund'] = round($rate_data['cr_age_total'] / $post['cr_ageid'], 0) + round($rate_data['cr_age_total'] * $back_rate / $post['cr_ageid'], 2); //农行萧山分行算法，月还款
                $rate_data['cr_age_money'] = number_format($rate_data['cr_age_total'] * $back_rate, 2, '.', ''); //农行萧山分行算法，分期手续费
                $rate_data['cr_refund_money'] = $rate_data['cr_age_total'] + $rate_data['cr_age_money']; //农行萧山分行算法，分期付款总额
                $rate_data['cr_refund_ratio'] = number_format(StringCode::ceil_format(($rate_data['cr_refund_money'] / $post['cr_money']) * 100), 2, '.', ''); ////农行萧山分行算法，分期付款总额比例
                $rate_data['cr_age_ratio'] = round(($rate_data['cr_age_total'] / $post['cr_money']) * 100, 2); //申请分期付款总额比例
            } else if ($post['cr_bank_money'] == '中国工商银行股份有限公司杭州延中支行' || $post['cr_bank_money'] == '中国工商银行股份有限公司南京城南支行' || $post['cr_bank_money'] == '（长马）中国工商银行股份有限公司南京城南支行') {//sunning 2016-06-29
                $rate_data['cr_refund_money'] = round($rate_data['cr_age_total'] * $back_rate + $rate_data['cr_age_total'], 2); //分期付款总额
                $rate_data['cr_refund_ratio'] = number_format(StringCode::ceil_format(($rate_data['cr_refund_money'] / $post['cr_money']) * 100), 2, '.', ''); //分期付款总额比例
                $rate_data['cr_age_money'] = $rate_data['cr_refund_money'] - $rate_data['cr_age_total']; //分期手续费
                $rate_data['cr_refund'] = ceil($rate_data['cr_refund_money'] / $post['cr_ageid']); //月还款
                $rate_data['cr_age_ratio'] = number_format(StringCode::ceil_format(($rate_data['cr_age_total'] / $post['cr_money']) * 100), 2, '.', ''); //申请分期付款总额比例
			} else if ($post['cr_bank_money'] == '（2017）中国工商银行股份有限公司杭州延中支行' || $post['cr_bank_money'] == '中国工商银行股份有限公司哈尔滨东直支行'){//sunning改2017
                //return array('success' => false, 'msg' => '公司费率不能低'.$company_rate.'于'.$back_rate.'银行利率'.bcadd($company_rate, - $back_rate, 6));
				$rate_data['cr_service_money'] = bcmul(bcadd($company_rate, - $back_rate, 5) , $post['cr_loan'], 2);//进百位
                //echo  $rate_data['cr_service_money'];
                $rate_data['cr_service_money'] = bcmul(ceil(bcdiv($rate_data['cr_service_money'],100, 4)),100);
				$rate_data['cr_service_per'] = bcdiv(bcmul($rate_data['cr_service_money'],100),$post['cr_money'],2);
                if($rate_data['cr_service_per']>15){
                    return array('success' => false, 'msg' => '金融服务费比例不能超15%');
                }
                //延中当实际贷款额大于50W，金融服务费比例不能大于10%
                if($post['cr_bank_money'] == '（2017）中国工商银行股份有限公司杭州延中支行'){
                    if($rate_data['cr_service_per']>10 && $data['ot_loan_limit']>500000){
                        return array('success' => false, 'msg' => '当实际贷款额大于50W，金融服务费比例不能大于10%');
                    }
                }
				$rate_data['cr_age_total'] = bcadd($post['cr_loan'], $rate_data['cr_service_money'], 2);
				$rate_data['cr_age_ratio'] = bcdiv(bcmul($rate_data['cr_age_total'],100),$post['cr_money'],2);
				$rate_data['cr_refund_money'] = bcmul(bcadd($post['cr_loan'], $rate_data['cr_service_money']), (1+$back_rate), 2);
				$rate_data['cr_refund_ratio'] = number_format(StringCode::ceil_format(($rate_data['cr_refund_money'] / $post['cr_money']) * 100), 2, '.', ''); //分期付款总额比例
				$rate_data['cr_refund'] = ceil($rate_data['cr_refund_money'] / $post['cr_ageid']); //月还款
				$rate_data['cr_age_money'] = $rate_data['cr_refund_money'] - $rate_data['cr_age_total']; //分期手续费
            } else {
                $rate_data['cr_refund_money'] = ceil($rate_data['cr_age_total'] * $back_rate + $rate_data['cr_age_total']); //分期付款总额
                $rate_data['cr_refund_ratio'] = number_format(StringCode::ceil_format(($rate_data['cr_refund_money'] / $post['cr_money']) * 100), 2, '.', ''); //分期付款总额比例
                $rate_data['cr_refund'] = ceil($rate_data['cr_refund_money'] / $post['cr_ageid']); //月还款
                $rate_data['cr_age_money'] = $rate_data['cr_refund_money'] - $rate_data['cr_age_total']; //分期手续费
                $rate_data['cr_age_ratio'] = number_format(StringCode::ceil_format(($rate_data['cr_age_total'] / $post['cr_money']) * 100), 2, '.', ''); //申请分期付款总额比例
            }
            if ($post['cr_genreid'] == '信用卡分期(利息一次性)') {
                /* 分期付款总额 = Math.Ceiling(实际贷款额 * (1 + 公司费率));
                  分期付款总额比例 = 分期付款总额 / 车价;
                  每月还款额 = Math.Ceiling(分期付款总额 / year);
                  分期手续费 = Math.Ceiling(分期付款总额 * 银行费率);
                  申请分期付款总额 = 分期付款总额;
                  申请分期付款总额比例 = 0; */
                $rate_data['cr_refund_money'] = ceil($post['cr_loan'] * $company_rate + $post['cr_loan']);
                $rate_data['cr_refund_ratio'] = number_format(StringCode::ceil_format(($rate_data['cr_refund_money'] / $post['cr_money']) * 100), 2, '.', '');
                $rate_data['cr_refund'] = ceil($rate_data['cr_refund_money'] / $post['cr_ageid']);
                $rate_data['cr_age_money'] = $rate_data['cr_refund_money'] * $back_rate;
                $rate_data['cr_age_total'] = $rate_data['cr_refund_money'];
                $rate_data['cr_age_ratio'] = $rate_data['cr_refund_ratio'];
            }
            //首期还款，月供
            if ($post['cr_bank_money'] == '中国工商银行股份有限公司南京城南支行' || $post['cr_bank_money'] == '中国工商银行股份有限公司哈尔滨东直支行') {
                $rate_data['cr_refund'] = floor(bcdiv($rate_data['cr_age_total'], $post['cr_ageid'], 5)) + floor(bcdiv($rate_data['cr_age_money'], $post['cr_ageid'], 5)); //月还款
                $rate_data['cr_refund_1'] = bcsub($rate_data['cr_refund_money'] ,bcmul($rate_data['cr_refund'],bcsub($post['cr_ageid'], 1)),2);
                if($post['cr_bank_money'] == '中国工商银行股份有限公司南京城南支行'){
                    $rate_data['cr_refund_1'] = ceil($rate_data['cr_refund_1']);
                }
            } else if($post['cr_bank_money'] == '（长马）中国工商银行股份有限公司南京城南支行'){
                $rate_data['cr_refund'] = floor($rate_data['cr_age_total'] / $post['cr_ageid']);
                $rate_data['cr_refund_1'] = ceil($rate_data['cr_refund_money'] - $rate_data['cr_refund'] * ($post['cr_ageid'] - 1));
            }else{
                $rate_data['cr_refund_1'] = $rate_data['cr_refund'];
            }


            /*

              $company_rate = floor($data['ot_loan_rate'] * 10000)/1000000;
              $loan_money_limit = ($rateinfo['data']['loan_limit'] / 100) * $data['ot_car_price'];  //可贷款金额
              $loan_money_limit = number_format($loan_money_limit, 2, '.', '');
              if ($data['ot_loan_limit'] > $loan_money_limit) {
              return array('success' => false, 'msg' => '贷款金额超出贷款比例' . $rateinfo['data']['loan_limit'] . '%');
              }
              if ($data['ot_loan_rate'] < $rateinfo['data']['rate']) {
              return array('success' => false, 'msg' => '公司费率不能低于银行利率' . $rateinfo['data']['rate'] . '%');
              }
              $d_payment = number_format($data['ot_car_price'] - $data['ot_loan_limit'], 2, '.', ''); //实际首付
              $d_payment_per = number_format(StringCode::ceil_format(($d_payment / $data['ot_car_price']) * 100), 2, '.', ''); //实际首付比例
              $d_loan_per = number_format(StringCode::ceil_format(($data['ot_loan_limit']/$data['ot_car_price'])*100), 2, '.', ''); //实际贷款比例
              $d_signag_money = ceil($data['ot_loan_limit'] * (1 + $data['ot_loan_rate'] / 100) / (1 + $rateinfo['data']['rate']) / 100) * 100; //申请分期付款总额
              $d_signag_money = floor(($data['ot_loan_limit'] * $company_rate + $data['ot_loan_limit']) / (1 + $back_rate)*10000)/1000000;

              $d_aging_moneyper = sprintf("%.2f", ($d_signag_money / $data['ot_car_price']) * 100); //申请分期付款总额比例
              $d_aging_money = ceil(($d_signag_money * (1 + $rateinfo['data']['rate']))); //分期付款总金额
              $d_aging_per = sprintf("%.2f", ($d_aging_money / $data['ot_car_price']) * 100); //分期付款比例

              $d_aging_poundage = $d_aging_money - $d_signag_money; //分期付款手续费
              $d_refund_month = ceil($d_aging_money / $data['ot_loan_year']); //月还款
              if ($data['ot_loan_type'] == '信用卡分期(利息一次性)') {
              $d_aging_money = ceil(($data['ot_loan_limit'] * (1 + $data['ot_loan_rate'] / 100))); //分期付款总金额
              $d_aging_per = sprintf("%.2f", ($d_aging_money / $data['ot_car_price']) * 100); //分期付款总额比例
              $d_refund_month = ceil($d_aging_money / $data['ot_loan_year']); //月还款
              $d_aging_poundage = ceil($d_aging_money * $rateinfo['data']['rate']); //分期付款手续费
              $d_signag_money = $d_aging_money;
              $d_aging_moneyper = $d_aging_per;
              }
              $result = array(
              'd_payment' => $d_payment, //实际首付
              'd_payment_per' => $d_payment_per, //实际首付比例
              'd_loan_per' => $d_loan_per, //实际贷款比例
              'd_aging_money' => $d_aging_money, //分期付款总金额
              'd_aging_per' => $d_aging_per, //分期付款比例
              'd_signag_money' => $d_signag_money, //申请分期付款总额
              'd_aging_poundage' => $d_aging_poundage, //分期付款手续费
              'd_aging_moneyper' => $d_aging_moneyper, //申请分期付款总额比例
              'd_refund_month' => $d_refund_month                 //月还款
              );
             * 
             */
            $result = array(
                'd_payment' => $rate_data['cr_down'], //实际首付
                'd_payment_per' => $rate_data['cr_down_ratio'], //实际首付比例
                'd_loan_per' => $rate_data['cr_loan_ratio'], //实际贷款比例
                'd_aging_money' => $rate_data['cr_refund_money'], //分期付款总金额
                'd_aging_per' => $rate_data['cr_refund_ratio'], //分期付款比例
                'd_signag_money' => $rate_data['cr_age_total'], //申请分期付款总额
                'd_aging_poundage' => $rate_data['cr_age_money'], //分期付款手续费
                'd_aging_moneyper' => $rate_data['cr_age_ratio'], //申请分期付款总额比例
                'd_refund_month' => $rate_data['cr_refund'], //月还款
                'd_refund_1month' => $rate_data['cr_refund_1'],
				'd_service_money' => $rate_data['cr_service_money'],
				'd_service_per' => $rate_data['cr_service_per'],
                'car_info' => array('ot_sinocap' => $car_row['ot_sinocap'], 'ot_estimate' => $car_row['ot_estimate'], 'ot_assessment' => $car_row['ot_assessment'])
            );
            if($data['o_bank_id']=='15' && $data['ot_car_pose'] == '二手车' ){
              $low_price = $data['ot_assessment'] > $data['ot_car_price']? $data['ot_car_price'] :$data['ot_assessment'];
              $price_ratio = bcdiv($rate_data['cr_age_total'],$low_price,4);
              if($price_ratio>0.7){
                $result['d_transaction_amount'] = ceil(bcdiv(bcdiv($rate_data['cr_age_total'],0.7,4),1000,7))*1000;
              }else{
                $result['d_transaction_amount'] = $low_price;
            }
          }
            if ($data['oid']) {
                $order_row = $model->getOneRow('order', array('o_pre_mode', 'o_branch_type', 'o_branch_id'), array('`o_id` = ?' => $data['oid']));
                $result['d_mode_info'] = $this->modeChange($order_row['o_pre_mode'], $data['ot_loan_limit']);
                if($order_row['o_branch_type'] == 'dot'){
                    $branch_row = $model->getOneRow('branch', '*', array('`br_id` = ?' => $order_row['o_branch_id']));
                    if(isset($branch_row['br_max_rate']) && is_numeric($branch_row['br_max_rate'])){
                        if($branch_row['br_max_rate'] < $data['ot_loan_rate']){
                            return array('success' => false, 'msg' => '公司费率超出系统设置比例' . $branch_row['br_max_rate'] . '%');
                        }
                    }
                }
            }
            if($car_price == 0){
                /**
                 * 结果校验
                 */
                $rate_chk = $this->rateDetail($data, $result);
                if(!$rate_chk['success']){
                    return array('success' => false, 'msg' => $rate_chk['msg']);
                }
            }
            if ($data['ot_car_pose'] == '二手车' && $car_price == 0 && $data['o_bank_id']!='8') {
                $re_result = $this->rateCompute($data, $ot_car_price);
                $result = $re_result['data'];
            }
            return array('success' => true, 'msg' => '请求成功', 'data' => $result);
        } else {
            return $rateinfo;
        }
    }

    /**
     * 返回响应产品的利率
     * @param type $data
     * @return type
     */
    public function rateBack($data) {
        $model = $this->LoadModel('Public');
        $bank_row = $model->getOneRow('bank', '*', array('`bk_id` = ?' => $data['o_bank_id']));
        $bank_inpro = empty($bank_row['bk_inpro']) ? array() : explode(',', $bank_row['bk_inpro']); //省内省份
        if (empty($bank_row)) {
            return array('success' => false, 'msg' => '未找到相关银行信息');
        }

        $order_row = $model->getOneRow('order', '*', array('`o_id` = ?' => $data['oid']));
        $data['o_bank_time'] = $order_row['o_bank_time'];

        $where = array(
            '`rt_bank_id` = ?' => $data['o_bank_id'],
            '`rt_type` = ?' => $data['ot_loan_type'],
            '`rt_up_time` <= ?' => date('Y-m-d', isset($data['o_bank_time']) && !empty($data['o_bank_time']) ? $data['o_bank_time']:time() ),
            '`rt_ylimit` = ?' => $data['ot_loan_year']
        );
        //$bank_rate = $model->getOneRow('bank_rate', '*', $where);
        $bank_rate = $model->getRow('bank_rate', '*', $where, null, array('rt_up_time DESC'));
        if (empty($bank_rate)) {
            return array('success' => false, 'msg' => '未找到相关银行执行利率');
        } else {
            if (in_array($data['ot_reg_province'], $bank_inpro)) {
                if ($data['ot_car_pose'] == '新车') {     //省内新车
                    $rate = $bank_rate['rt_newrate_in'];
                    $code = $bank_rate['rt_newcode_in'];
                    $loan_limit = $bank_row['bk_newloan_in'];//上限
                    $loan_f_limit = $bank_row['bk_newratio_in'];
                    $loan_s_limit = $bank_row['bk_newstage_in'];
                    $rate_id = $bank_rate['rt_id'];
                } else {                                  //省内二手车
                    $rate = $bank_rate['rt_secrate_in'];
                    $code = $bank_rate['rt_seccode_in'];
                    $loan_limit = $bank_row['bk_secloan_in'];
                    $loan_f_limit = $bank_row['bk_secratio_in'];
                    $loan_s_limit = $bank_row['bk_secstage_in'];
                    $rate_id = $bank_rate['rt_id'];
                }
            } else {
                if ($data['ot_car_pose'] == '新车') {     //省外新车
                    $rate = $bank_rate['rt_newrate_out'];
                    $code = $bank_rate['rt_newcode_out'];
                    $loan_limit = $bank_row['bk_newloan_out'];
                    $loan_f_limit = $bank_row['bk_newratio_out'];
                    $loan_s_limit = $bank_row['bk_newstage_out'];
                    $rate_id = $bank_rate['rt_id'];
                } else {                                  //省外二手车
                    $rate = $bank_rate['rt_secrate_out'];
                    $code = $bank_rate['rt_seccode_out'];
                    $loan_limit = $bank_row['bk_secloan_out'];
                    $loan_f_limit = $bank_row['bk_secratio_out'];
                    $loan_s_limit = $bank_row['bk_secstage_out'];
                    $rate_id = $bank_rate['rt_id'];
                }
            }
        }
        if($bank_row['bk_name'] == '（长马）中国工商银行股份有限公司南京城南支行'){
            $company_rate = ($data['ot_loan_year'] == 36 && $data['ot_loan_type']=='信用卡分期(利息分期)') ? 4 : 0;
        }
        return array('success' => true, 'msg' => '获取成功', 'data' => array('rate' => $rate / 100, 'code' => $code, 'loan_limit' => $loan_limit, 'loan_f_limit' => $loan_f_limit, 'loan_s_limit' => $loan_s_limit, 'rt_id' => $rate_id, 'bank' => $bank_row['bk_name'], 'ylimit' => $data['ot_loan_year'], 'company_rate' => $company_rate));
    }

    public function modeChange($source_mode, $loan_money) {
        $mode = $this->loan_mode[$source_mode];
        return $mode;
        /*
        if ($loan_money > $mode['loan_max']) {
            return $this->loan_mode[1];
        } else {
            return $mode;
        }*/
    }

    /**
     * 利率校验
     * @return type
     */
    public function rateCheck($data) {
        $rate_calculate = $this->rateCompute($data);
        if (!$rate_calculate['success']) {
            return $rate_calculate;
        }
        $rate_calculate = $rate_calculate['data'];
        if($data['ot_assessment'] != $rate_calculate['car_info']['ot_assessment']){
            return array('success' => false, 'msg' => '评估价已改变，请重新计算后提交');
        }
        $rate_chk = $this->rateDetail($data, $rate_calculate);
        if(!$rate_chk['success']){
            return array('success' => false, 'msg' => $rate_chk['msg']);
        }
        //验证银行上牌地 避免APP端不同步
        $model = $this->LoadModel('Public');
        $city_msg = $model->getOneRow('city','*',array('`city` =?' =>$data['ot_reg_city']));
        $citybank_arr = explode(',',$city_msg['admittance_bank']);
        $citybranch_arr = explode(',',$city_msg['unadmittance_branch']);
        if(!empty($data['ot_reg_area'])){
            $area_msg = $model->getOneRow('city','*',array('`city` =?' =>$data['ot_reg_area']));
            $areabank_arr = explode(',',$area_msg['admittance_bank']);
            $areabranch_arr = explode(',',$area_msg['unadmittance_branch']);
            if(!in_array($data['o_bank_id'],$areabank_arr)){
                return array('success' =>false , 'msg' => '当前县或者区不支持为上牌地');
            }
        }
        if(!in_array($data['o_bank_id'],$citybank_arr)){
            return array('success' =>false , 'msg' =>'当前市不支持为上牌地');
        }
        //网点上牌地验证
        $order_row = $model->getOneRow('order','*',array('`o_id` =?' => $data['oid']));
        $branch_msg = $model->getOneRow('branch','*',array('`br_id` =?' =>$order_row['o_branch_id']));
        if(!in_array($branch_msg['br_id'],$citybranch_arr)){
            return array('success' =>false, 'msg' =>'当前市不在你网点允许的区域，如有异议请联系我司分管抵押事业部');
        }
        if(!empty($data['ot_reg_area'])){
            if(!in_array($branch_msg['br_id'],$areabranch_arr)){
                return array('success' =>false, 'msg' =>'当前区或者县不在你网点允许的区域，如有异议请联系我司分管抵押事业部');
            }
        }
        /*
        if($data['ot_reg_city']=='台州市'){
            return array('success'=>false,'msg'=>'当前市不支持为上牌地');
        }
        if($data['ot_reg_province']=='浙江省' && $data['o_bank_id']==8){
            return array('success'=>false,'msg'=>'当前省不支持为上牌地');
        }
        if($data['o_bank_id']==13){
            $arr=array(
                '杭州市',
                '嘉兴市',
                '衢州市',
                '湖州市',
                '温州市',
                '绍兴市',
                '宁波市',
                '金华市',
                '丽水市',
                '芜湖市',
                '蚌埠市',
                '淮南市',
                '马鞍山市',
                '淮北市',
                '铜陵市',
                '黄山市',
                '安庆市',
                '阜阳市',
                '宿州市',
                '滁州市',
                '六安市',
                '福州市',
                '漳州市',
                '南平市',
                '龙岩市',
                '宁德市',
                '莆田市',
                '泉州市',
                '上饶市',
                '赣州市',
                '鹰潭市',
                '九江市',
                '抚州市',
                '宜春市',
                '吉安市',
                '景德镇市',
                '萍乡市',
                '新余市',
                '成都市',
                '绵阳市',
                '泸州市',
                '德阳市',
                '内江市',
                '达州市',
                '雅安市',
                '甘孜藏族自治州',
                '巴中市',
                '资阳市',
                '遂宁市',
                '乐山市',
                '南充市',
                '泸州市',
                '自贡市',
                '广元市',
                '宜宾市',
                '天津市',
                '西安市',
                '乌鲁木齐市',
                '伊犁哈萨克自治州',
                '大连市',
                '营口市',
                '佳木斯市',
                '大庆市',
                '长春市',
                '吉林市',
                '四平市',
                '白城市',
                '松原市',
                '延边州',
                '白山市',
                '通化市',
                '辽源市',
                '郑州市',
                '濮阳市',
                '南阳市',
                '漯河市',
                '许昌市',
                '信阳市',
                '驻马店市',
                '洛阳市',
                '平顶山市',
                '商丘市',
                '焦作市',
                '茂名市',
                '惠州市',
                '河源市',
                '梅州市',
                '汕头市',
                '揭阳市',
                '潮州市',
                '深圳市',
                '韶关市',
                '清远市',
                '汕尾市',
                '石家庄市',
                '唐山市',
                '昆明市',
                '大理白族自治州',
                '红河哈尼族彝族自治州',
                '文山壮族苗族自治州',
                '曲靖市',
                '平凉市',
                '庆阳市',
                '吴忠市',
                '石嘴山市',
                '银川市',
                '中卫市',
                '固原市',
                '长沙市',
                '常德市',
                '岳阳市',
                '阳江市',
                '南昌市',
                '江门市',
                '佛山市',
                '开封市',
                '宣城市',
                '眉山市',
                '本溪市',
                '安阳市',
                '凉山彝族自治州',
                '周口市',
                '沈阳市',
                '安康市',
                '漳州市',
                '榆林市',
                '渭南市',
                '抚顺市',
                '盘锦市',
                '商洛市',
                '鹤壁市',
                '三门峡市',
                '济源市',
                '新乡市',
                '运城市',
                '太原市',
                '临汾市',
                '哈尔滨市',
                '绥化市',
                '齐齐哈尔市',
                '双鸭山市',
                '鹤岗市',
                '石河子市',
                '延安市',
                '铁岭市',
                '亳州市'
            );
            $arrcity=array(
                '合肥市',
                '肇庆市',
                '云浮市',
                '湛江市',
                '其它'
            );
            $arrarea=array(
                '肥东县',
                '长丰县',
                '肥西县',
                '包河区',
                '端州区',
                '高要市',
                '四会市',
                '鼎湖区',
                '广宁县',
                '怀集县',
                '罗定市',
                '云城区',
                '新兴县',
                '赤坎区',
                '雷州市',
                '麻章区',
                '坡头区',
                '遂溪县',
                '霞山区',
                '徐闻县'
            );
            if(!in_array($data['ot_reg_city'],$arr)){
                if(!in_array($data['ot_reg_city'],$arrcity)){
                    return array('success'=>false,'msg'=>'当前市不支持为上牌地');
                }else{
                    if(!in_array($data['ot_reg_area'],$arrarea)){
                        return array('success'=>false,'msg'=>'当前区不支持为上牌地');
                    }
                }
            }

        }
        if($data['o_bank_id'] == 11){
          if($data['ot_reg_aera'] =='滑县'){  //仅南京不支持
            return array('success'=>false,'msg'=>'当前县不支持为上牌地');
          }elseif($data['ot_reg_city'] == '长沙市'){
            return array('success'=>false,'msg'=>'当前市不支持为上牌地');
          }
        }
        $arr_city=array('揭阳市','肇庆市','云浮市','珠海市','中山市','龙岩市','咸阳市');
        $arr_city_r=array('梅州市','湛江市');
        $arr_area=array('饶平县','平远县','吴川市','雷州市','遂溪县','徐闻县');
        $ha_city_arr = array('韶关市','江门市','潮州市'); //仅哈工可以
        if(in_array($data['ot_reg_city'],$ha_city_arr) && $data['o_bank_id'] !=15){
          return array('success'=>false,'msg'=>'当前市不支持为上牌地');
        }
        if(in_array($data['ot_reg_city'],$arr_city)) return array('success'=>false,'msg'=>'当前市不支持为上牌地');
        if(in_array($data['ot_reg_city'],$arr_city_r)){
            if(in_array($data['ot_reg_area'],$arr_area)) return array('success'=>false,'msg'=>'当前县不支持为上牌地');
        }*/


        return array('success' => true, 'msg' => '校验通过', 'data' => $rate_calculate);

        /*
        $bank_info = $this->bankRow($data['o_bank_id']);
        $bank_inpro = empty($bank_info['bk_inpro']) ? array() : explode(',', $bank_info['bk_inpro']); //省内省份
        if (in_array($data['ot_reg_province'], $bank_inpro)) {
            $d_aging_moneyper = $data['ot_car_pose'] == '新车' ? $bank_info['bk_newstage_in'] : $bank_info['bk_secstage_in']; //申请分期付款总额比例
            $d_aging_per = $data['ot_car_pose'] == '新车' ? $bank_info['bk_newratio_in'] : $bank_info['bk_secratio_in']; //分期付款总额比例
        } else {
            $d_aging_moneyper = $data['ot_car_pose'] == '新车' ? $bank_info['bk_newstage_out'] : $bank_info['bk_secstage_out']; //申请分期付款总额比例
            $d_aging_per = $data['ot_car_pose'] == '新车' ? $bank_info['bk_newratio_out'] : $bank_info['bk_secratio_out']; //分期付款总额比例
        }
        if ($rate_calculate['d_aging_moneyper'] > $d_aging_moneyper) {
            return array('success' => false, 'msg' => '申请分期付款总额比例超出银行比例设置');
        }
        if ($rate_calculate['d_aging_per'] > $d_aging_per) {
            return array('success' => false, 'msg' => '分期付款总额比例超出银行比例设置');
        }
        return array('success' => true, 'msg' => '校验通过', 'data' => $rate_calculate);
         * 
         */
    }
    /**
     * 
     * @param type $data
     * @param type $rate_calculate //rateCheck计算结果
     * @return type
     */
    public function rateDetail($data, $rate_calculate){
        $bank_info = $this->bankRow($data['o_bank_id']);
        $bank_inpro = empty($bank_info['bk_inpro']) ? array() : explode(',', $bank_info['bk_inpro']); //省内省份
        if (in_array($data['ot_reg_province'], $bank_inpro)) {
            $d_aging_moneyper = $data['ot_car_pose'] == '新车' ? $bank_info['bk_newstage_in'] : $bank_info['bk_secstage_in']; //申请分期付款总额比例
            $d_aging_per = $data['ot_car_pose'] == '新车' ? $bank_info['bk_newratio_in'] : $bank_info['bk_secratio_in']; //分期付款总额比例
        } else {
            $d_aging_moneyper = $data['ot_car_pose'] == '新车' ? $bank_info['bk_newstage_out'] : $bank_info['bk_secstage_out']; //申请分期付款总额比例
            $d_aging_per = $data['ot_car_pose'] == '新车' ? $bank_info['bk_newratio_out'] : $bank_info['bk_secratio_out']; //分期付款总额比例
        }
        if ($d_aging_moneyper > 0 && $rate_calculate['d_aging_moneyper'] > $d_aging_moneyper) {
            return array('success' => false, 'msg' => '申请分期付款总额比例超出银行比例设置');
        }
        if ($d_aging_per > 0 && $rate_calculate['d_aging_per'] > $d_aging_per) {
            return array('success' => false, 'msg' => '分期付款总额比例超出银行比例设置');
        }
        /**
         * 补充校验
         */
        if(isset($data['ot_calculated']) && !empty($data['ot_calculated']) && $data['ot_car_pose'] == '新车'){
            if(!is_array($data['ot_calculated'])) $data['ot_calculated'] = unserialize ($data['ot_calculated']);
            foreach ($data['ot_calculated'] as $k => $v){
                if(isset($rate_calculate[$k]) && bcadd($rate_calculate[$k], -$v, 2) != 0){
                    return array('success' => false, 'msg' => '利率校验值不一致,请重新计算后提交');
                }
            }
        }
        if($data['ot_calculated']){
            $check_arr = array('d_payment', 'd_payment_per', 'd_loan_per', 'd_aging_money', 'd_aging_per', 'd_aging_poundage', 'd_signag_money', 'd_aging_moneyper', 'd_refund_month', 'd_refund_1month');
            if(!is_array($data['ot_calculated'])) $data['ot_calculated'] = unserialize ($data['ot_calculated']);
            foreach($check_arr as $k => $v){
                if(!isset($data['ot_calculated'][$v]) || !is_numeric($data['ot_calculated'][$v])){
                    return array('success' => false, 'msg' => '利率值不完整，请重新计算后提交');
                }
            }
        }
        return array('success' => true, 'msg' => '校验通过');
    }

     /**
     * 银行网点分页
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function branList($where,$page = 1, $limit = PAGE_SIZE, $param = array()){
        $where['`br_status` = ?'] = 'ok';
        if(!empty($param['o_branch'])){
            $where['`br_name` LIKE ?'] = '%'.$param['o_branch'].'%';
        }
        $model = $this->LoadModel('Public');
        $data['count'] = $model->getRowCount('branch',$where);
        if(!empty($data['count'])){
            $data['all'] = $model->getList('branch', '*', $where, array($page, $limit), NULL, array('br_rgid ASC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }
}